package com.dps.recommend.layer.merage;


import com.dps.recommend.ann.LayerAnn;
import com.dps.recommend.beans.DpsRequest;
import com.dps.recommend.beans.ProcessInterface;
import com.dps.recommend.beans.RecItemDps;
import com.dps.recommend.enums.LayerEnum;
import com.dps.recommend.utils.DpsBaseService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 融合 根据itemId去重复并且处理recallVersion为cn以,分割
 */
@Service
@LayerAnn(layer = LayerEnum.MERAGE)
public class DpsItemIdCnV1Merage extends DpsBaseService implements ProcessInterface {


    @Override
    public <R extends RecItemDps> List doProcess(DpsRequest dpsRequest, List<R> list) {
        if (CollectionUtils.isEmpty(list)) {
            return list;
        }
        list = list.stream().filter(Objects::nonNull).collect(Collectors.toMap(R::getItemId, a -> a, (o1, o2) -> {
            String recallVersion = o1.getRecallVersion();
            recallVersion = StringUtils.isEmpty(recallVersion) ? o1.getCn() + "," + o2.getCn() : recallVersion.contains(o1.getCn()) ? recallVersion : recallVersion + "," + o1.getCn();
            recallVersion = StringUtils.isEmpty(recallVersion) ? o1.getCn() + "," + o2.getCn() : recallVersion.contains(o2.getCn()) ? recallVersion : recallVersion + "," + o2.getCn();
            o1.setRecallVersion(recallVersion);
            return o1;
        })).values().stream().map(recItemSquare -> {
            String recallVersion = recItemSquare.getRecallVersion();
            recItemSquare.setRecallVersion(StringUtils.isEmpty(recallVersion) ? recItemSquare.getCn() : recallVersion);
            return recItemSquare;
        }).collect(Collectors.toList());

        return list;
    }
}
